﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;

namespace WSSGServer
{
    public class WSSGData
    {
        string m_Dir;
        string m_File;
        string m_Name;
        private TimeSpan m_StarLive;
        List<WSSGObject> m_ObjectList;
        List<StarObject> m_StarObject;
        public string Dir
        {
            get { return m_Dir; }
            set { m_Dir = value; }
        }
        public string File
        {
            get { return m_File; }
            set { m_File = value; }
        }
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }       
        [XmlArray]
        [XmlArrayItem(typeof(WSSGObject))]
        public List<WSSGObject> ObjectList
        {
            get { return m_ObjectList; }
            set { m_ObjectList = value;
            CreateObjList();
            }
        }
        public TimeSpan StarLive
        {
            get { return m_StarLive; }
            set { m_StarLive = value; }
        }        
        public void CreateObjList()
        {
            m_StarObject.Clear();
            for (int i = 0; i < m_ObjectList.Count; i++)
            {
                WSSGObject obj = m_ObjectList[i];
                m_StarObject.Add(new StarObject() { ID = obj.ID, Name = obj.Name, Dirictory = obj.Directory });                
            }
        }
        public void AddObject(WSSGObject obj)
        {
            m_ObjectList.Add(obj);
            m_StarObject.Add(new StarObject() { ID = obj.ID, Name = obj.Name, Dirictory = obj.Directory });
        }
        [XmlIgnore]
        public List<StarObject> StarsObjects
        {
            get { return m_StarObject; }
            set { m_StarObject = value; }
        }        
        public WSSGData(string dir,string File,string name)
        {
            m_Dir = dir;
            m_File = File;
            m_Name = name;
            m_ObjectList = new List<WSSGObject>(0);
            m_StarObject = new List<StarObject>(0);
            m_StarLive = new TimeSpan(0);
        }
        public WSSGData(string dir, string File)
        {
            m_Dir = dir;
            m_File = File;
            m_ObjectList = new List<WSSGObject>(0);
            m_StarObject = new List<StarObject>(0);
            m_StarLive = new TimeSpan(0);
        }
        public WSSGData()
        {
            m_ObjectList = new List<WSSGObject>(0);
            m_StarObject = new List<StarObject>(0);
            m_StarLive = new TimeSpan(0);
        }
        internal bool Save()
        {
            try
            {
                //Сериализуем полученное
                XmlWriter writer = new XmlTextWriter(m_Dir+"/" + m_File, System.Text.Encoding.UTF8);
                XmlSerializer serializer = new XmlSerializer(typeof(WSSGData));
                serializer.Serialize(writer, this);
                writer.Close();
                Console.WriteLine("Система успешно сохранена в файл: " + m_Dir + "/" + m_File);
                Console.WriteLine("Сохраняем объекты системы `"+m_Name+"`" );
                for (int i = 0; i < m_ObjectList.Count; i++)
                {
                   m_StarObject[m_ObjectList[i].ID].Save();
                }
                Console.WriteLine("Объекты системы `" + m_Name + "` сохранены.");
             
                return true;
            }
            catch (Exception exc)
            {
                Console.WriteLine("Ошибка: " + exc.Message);
                return false ;
            }
        }

        static internal WSSGData Load(string dir, string File)
        {
            try
            {
                XmlReader reader = new XmlTextReader(dir + "/" + File);
                XmlSerializer serializer = new XmlSerializer(typeof(WSSGData));
                WSSGData data = (WSSGData)serializer.Deserialize(reader);
                reader.Close();
                data.CreateObjList();
                Console.WriteLine("Система успешно загружена из файла: " + dir + "/" + File);
                Console.WriteLine("Загружаем объекты системы `" + data.Name + "`");
                for (int i = 0; i < data.ObjectList.Count; i++)
                {
                    data.StarsObjects[data.ObjectList[i].ID].Load();
                }
                Console.WriteLine("Объекты системы `" + data.Name + "` загружены.");
                return data;
            }
            catch (Exception exc)
            {
                Console.WriteLine("Ошибка: "+exc.Message );
                return null;
            }
            
        }

        internal void CreateNewSystem(int sunCount, int astCount)
        {
            for (int i = 0; i < sunCount; i++)
            {
               AddObject(new WSSGObject("Sun " + i,ObjectList.Count, m_Dir + "/SunsDir", "Sun " + i + ".wssg", StarObjectType.Sun));
            }
            for (int i = 0; i < astCount; i++)
            {
                AddObject(new WSSGObject("Asteroid " + i, ObjectList.Count, m_Dir + "/AsteroidDir", "Asteroid " + i + ".wssg", StarObjectType.Asteroid));  
            }
         
              
        }
    }
}
